package com.hmxx.myblogproject.service.impl;

import com.hmxx.myblogproject.entity.MyBlogFollow;
import com.hmxx.myblogproject.entity.MyBlogUser;
import com.hmxx.myblogproject.mapper.MyBlogFollowMapper;
import com.hmxx.myblogproject.mapper.MyBlogUserMapper;
import com.hmxx.myblogproject.service.MyBlogFollowService;
import com.hmxx.myblogproject.util.JwtHelper;
import com.hmxx.myblogproject.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @Author: {27106}
 * @Date: {2021/11/25 10:31}
 */
@Service
public class MyBlogFollowServiceImpl implements MyBlogFollowService {
    @Resource
    private MyBlogFollowMapper myBlogFollowMapper;
    @Resource
    private MyBlogUserMapper myBlogUserMapper;

    @Override
    public Object setFollow(MyBlogFollow myBlogFollow) {
        if (null == myBlogFollow.getFollowedUserId() || null == myBlogFollow.getFansUserId()) {
            return ResponseUtil.fail(2, "参数错误");
        }

        MyBlogFollow result = myBlogFollowMapper.queryFollow(
                myBlogFollow.getFollowedUserId(), myBlogFollow.getFansUserId()
        );

        //取消关注
        if (null != result) {
            int res = myBlogFollowMapper.cancelFollow(
                    myBlogFollow.getFollowedUserId(), myBlogFollow.getFansUserId()
            );
            if (res > 0) {
                MyBlogUser user = myBlogUserMapper.queryUserById(myBlogFollow.getFollowedUserId());
                int size=myBlogFollowMapper.queryFollowsByUId(user.getId());
                user.setFans(String.valueOf(size));
                return ResponseUtil.ok(2, user);
            } else {
                return ResponseUtil.fail(1, "取关失败");
            }
        }

        int r = myBlogFollowMapper.insertFollow(myBlogFollow);

        if (r > 0) {
            MyBlogUser user = myBlogUserMapper.queryUserById(myBlogFollow.getFollowedUserId());
            int size=myBlogFollowMapper.queryFollowsByUId(user.getId());
            user.setFans(String.valueOf(size));
            return ResponseUtil.ok(1, user);
        } else {
            return ResponseUtil.fail(1, "数据库插入失败");
        }

    }

    @Override
    public Object getFollowStatus(MyBlogFollow myBlogFollow) {
        if (null == myBlogFollow.getFollowedUserId() || null == myBlogFollow.getFansUserId()) {
            return ResponseUtil.fail(2, "参数错误");
        }
        MyBlogFollow result = myBlogFollowMapper.queryFollow(
                myBlogFollow.getFollowedUserId(), myBlogFollow.getFansUserId()
        );
        if (null != result) {
            return ResponseUtil.ok(1, "已关注");
        } else {
            return ResponseUtil.ok(2, "未关注");
        }
    }

    @Override
    public Object getFollow(HttpServletRequest request) {
        if (null != JwtHelper.getUserId(request.getHeader("MyBlog-Token"))) {
            String uId = String.valueOf(JwtHelper.getUserId(request.getHeader("MyBlog-Token")));
            List<MyBlogUser> follows = myBlogFollowMapper.queryFansByUid(uId);
            return ResponseUtil.okList(follows);
        } else {
            return ResponseUtil.fail(1, "登录过期，请重新登录");
        }
    }
}
